using System.Collections.Generic;
using NUnit.Framework;
using Rhino.Mocks;
using ValidationFramework.Tests.TestHelpers;

namespace ValidationFramework.Tests
{
	public sealed class ResultFormatterTest
	{

		[TestFixture]
		public class GetErrorMessages
		{

			private const string expectedErrorMessage1 = "ErrorMessage1";
			private const string expectedErrorMessage2 = "Error\r\nMessage2";

			[Test]
			public void Simple()
			{
				var mockRule = MockRepository.GenerateStub<Rule>(new AlwaysFailValidator());
				var fakeDescriptor = new FakeReflectionInfo();
				var validationResults = new List<ValidationError>
                   {
                       new ValidationError(mockRule, expectedErrorMessage1, null, fakeDescriptor),
                       new ValidationError(mockRule, expectedErrorMessage2, null, fakeDescriptor)
                   };

				var messages = ResultFormatter.GetErrorMessages(validationResults);
				Assert.AreEqual(expectedErrorMessage1, messages[0]);
				Assert.AreEqual(expectedErrorMessage2, messages[1]);
			}
			[Test]
			public void SplitOnNewLine()
			{
				var mockRule = MockRepository.GenerateStub<Rule>(new AlwaysFailValidator());
				var fakeDescriptor = new FakeReflectionInfo();
				var validationResults = new List<ValidationError>
				   {
					   new ValidationError(mockRule, expectedErrorMessage1, null, fakeDescriptor),
					   new ValidationError(mockRule, expectedErrorMessage2, null, fakeDescriptor)
				   };
				var messages = ResultFormatter.GetErrorMessages(validationResults, true);
				Assert.AreEqual(expectedErrorMessage1, messages[0]);
				Assert.AreEqual("Error", messages[1]);
				Assert.AreEqual("Message2", messages[2]);
			}
		}


		[TestFixture]
		public class GetConcatenatedErrorMessages
		{

			private const string expectedErrorMessage1 = "ErrorMessage1";
			private const string expectedErrorMessage2 = "ErrorMessage2";

			[Test]
			public void Simple()
			{
				var mockRule = MockRepository.GenerateStub<Rule>(new AlwaysFailValidator());
				var fakeDescriptor = new FakeReflectionInfo();
				var validationResults = new List<ValidationError> 
				{
					new ValidationError(mockRule, expectedErrorMessage1, null, fakeDescriptor), 
					new ValidationError(mockRule, expectedErrorMessage2, null, fakeDescriptor)
				};

				var concatenatedErrorMessages = ResultFormatter.GetConcatenatedErrorMessages(validationResults);
				Assert.AreEqual("ErrorMessage1\r\nErrorMessage2", concatenatedErrorMessages);
			}
			[Test]
			public void StringCollection()
			{
				var validationResults = new List<string> { expectedErrorMessage1, expectedErrorMessage2 };

				var concatenatedErrorMessages = ResultFormatter.GetConcatenatedErrorMessages(validationResults);
				Assert.AreEqual("ErrorMessage1\r\nErrorMessage2", concatenatedErrorMessages);
			}
			[Test]
			public void SimpleSeperator()
			{
                var mockRule = MockRepository.GenerateStub<Rule>(new AlwaysFailValidator());
				var fakeDescriptor = new FakeReflectionInfo();
				var validationResults = new List<ValidationError>
               {
                   new ValidationError(mockRule, expectedErrorMessage1, null, fakeDescriptor),
                   new ValidationError(mockRule, expectedErrorMessage2, null, fakeDescriptor)
               };

				var concatenatedErrorMessages = ResultFormatter.GetConcatenatedErrorMessages(",", validationResults);
				Assert.AreEqual("ErrorMessage1,ErrorMessage2", concatenatedErrorMessages);
			}
			[Test]
			public void StringCollectionSeperator()
			{
				var validationResults = new List<string> { expectedErrorMessage1, expectedErrorMessage2 };

				var concatenatedErrorMessages = ResultFormatter.GetConcatenatedErrorMessages(",", validationResults);
				Assert.AreEqual("ErrorMessage1,ErrorMessage2", concatenatedErrorMessages);
			}
		}

	}
}